package com.applink.sell_proof.database.table;

import android.content.ContentResolver;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

import com.applink.sell_proof.EMarketing;
import com.applink.sell_proof.database.EMarketingContentProvider;
import com.applink.sell_proof.database.EMarketingDatabaseHelper;
import com.applink.sell_proof.model.Quiz;

public class QuizDBHelper {
	public static final String TABLE_NAME = "quiz_table";
	public static final String QUIZ_ID = "id";
	public static final String DESCRIPTION = "description";
	public static final String NAME = "name";
	public static final String START_DATE = "start_date";
	public static final String END_DATE = "end_date";
	public static final String QUIZ_CATEGORY = "category";
	public static final String SUBMITTED = "submitted";

	public static final String SQL_EXCUTE = "CREATE TABLE " + TABLE_NAME + "("
			+ QUIZ_ID + " integer not null primary key ," + DESCRIPTION
			+ " text ," + NAME + " text ," + START_DATE + " integer ,"
			+ END_DATE + " integer ," + SUBMITTED + " integer ,"
			+ QUIZ_CATEGORY + " integer " + ");";

	public static void createQuiz(Quiz quiz) {
		ContentValues value = new ContentValues();
		value.put(QUIZ_ID, quiz.getId());
		value.put(DESCRIPTION, quiz.getDescription());
		value.put(NAME, quiz.getName());
		value.put(END_DATE, quiz.getEndDate());
		value.put(START_DATE, quiz.getStartDate());
		value.put(QUIZ_CATEGORY, quiz.getCategory().toInt());
		value.put(SUBMITTED, quiz.isSubmitted() ? 1 : 0);

		ContentResolver resolver = EMarketing.getInstance()
				.getApplicationContext().getContentResolver();
		resolver.insert(EMarketingContentProvider.QUIZ_CONTENT_URI, value);
	}

	public static Quiz getQuiz(long quizId) {
		SQLiteOpenHelper dbHelper = new EMarketingDatabaseHelper(EMarketing
				.getInstance().getApplicationContext());
		SQLiteDatabase db = dbHelper.getWritableDatabase();
		ContentResolver resolver = EMarketing.getInstance()
				.getApplicationContext().getContentResolver();
		Cursor cursor = resolver.query(
				EMarketingContentProvider.QUIZ_CONTENT_URI, null, QUIZ_ID
						+ "=?", new String[] { Long.toString(quizId) }, null);
		if (cursor != null) {
			try {
				cursor.moveToFirst();
				Quiz quiz = new Quiz();
				quiz.setId(quizId);
				quiz.setName(cursor.getString(cursor.getColumnIndex(NAME)));
				quiz.setDescription(cursor.getString(cursor
						.getColumnIndex(DESCRIPTION)));
				quiz.setEndDate(cursor.getLong(cursor.getColumnIndex(END_DATE)));
				quiz.setStartDate(cursor.getLong(cursor
						.getColumnIndex(START_DATE)));
				quiz.setStartDate(cursor.getLong(cursor
						.getColumnIndex(START_DATE)));

				return quiz;

			} finally {
				cursor.close();
				db.close();
			}
		}
		return null;
	}

	public static void updateSubmitted(long quizId, boolean isSubmitted) {
		ContentValues values = new ContentValues();
		values.put(SUBMITTED, isSubmitted ? 1 : 0);
		ContentResolver resolver = EMarketing.getInstance()
				.getApplicationContext().getContentResolver();
		resolver.update(EMarketingContentProvider.QUIZ_CONTENT_URI, values,
				QUIZ_ID + "=?", new String[] { Long.toString(quizId) });
	}
}
